(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 1;
    vc.extends({
        data: {
            orderInfo: {
                "appId": "",
                "freightPrice": "",
                "oId": "",
                "orderId": "",
                "personId": "",
                "personName": "",
                "records": 0,
                "remark": "",
                "stateName": "",
                "storeOrderAddressDto": {},
                "storeOrderCartDtos": []
            },
            timer: null, // 定时器对象
            stopPolling: false // 是否停止调用接口的标志
        },
        _initMethod: function () {
            vc.component._listOrders(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {

        },
        methods: {
            //获取用户订单
            _listOrders: function (_page, _rows) {
                var param = {
                    params: {page:_page,row:_rows,orderId:vc.getParam("orderId")}
                };
                //发送get请求
                vc.http.apiGet('storeOrder.listStoreOrder',
                    param,
                    function (json, res) {
                        var _orderInfo = JSON.parse(json);
                        if(_orderInfo.data.length == 0){
                            vc.toast("订单号不正确，请正确使用");
                        }else{
                            vc.component.orderInfo = _orderInfo.data[0];
                            if(vc.component.state =="0"){
                                vc.jumpToPage("/pageLR.html#/pages/plateform/paySuccess?orderId=" + vc.getParam("orderId"));
                                return;
                            }
                            vc.component._caculTotalMount();
                            $that._getQrCode(vc.component.orderInfo);
                            $that.startPolling(_page, _rows);
                        }
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _getQrCode: function(_order) {
                if (_order.state !="1"){
                    vc.jumpToPage("/pageLR.html#/pages/plateform/payResult?orderId=" + _order.orderId);
                }
                let _data = {
                    orderId: _order.orderId,
                    money: _order.totalPrice,
                    name: _order.remark
                }
                vc.http.apiPost(
                    '/payment.nativeQrcodePayment',
                    JSON.stringify(_data), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.toast("请使用微信扫描二维码支付");
                            $that._viewQr(_json.codeUrl);
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });

            },
            startPolling(_page, _rows) {
                const timer = setInterval(() => {
                    var param = {
                        params: {page:_page,row:_rows,orderId:vc.getParam("orderId")}
                    };
                    //发送get请求
                    vc.http.apiGet('storeOrder.listStoreOrder',
                        param,
                        function (json, res) {
                            var _orderInfo = JSON.parse(json);
                            if(_orderInfo.data.length == 0){
                                vc.toast("订单号不正确，请正确使用");
                            }else{
                                let _order = _orderInfo.data[0];
                                if(_order.state !="1"){// 已支付的状态
                                    clearInterval(timer); // 清除定时器
                                    vc.jumpToPage("/pageLR.html#/pages/plateform/payResult?orderId=" + _order.orderId);
                                    return;
                                }
                            }
                        }, function (errInfo, error) {
                            clearInterval(timer); // 清除定时器
                            console.log('请求失败处理');
                        }
                    );
                }, 5000); // 每隔5秒调用一次接口
              },
              callApi() {
                // 调用接口的逻辑，返回一个Promise对象
                return new Promise((resolve, reject) => {
                  // 模拟接口调用，假设接口返回一个随机数
                  setTimeout(() => {
                    const randomNum = Math.random();
                    console.log('接口返回', randomNum);
                    resolve({ data: randomNum });
                  }, 1000); // 假设接口调用需要1秒钟
                });
              },
            _viewQr: function (_url) {
                document.getElementById("qrcode").innerHTML = "";
                let qrcode = new QRCode(document.getElementById("qrcode"), {
                    text: "133", //你想要填写的文本
                    width: 146, //生成的二维码的宽度
                    height: 146, //生成的二维码的高度
                    colorDark: "#000000", // 生成的二维码的深色部分
                    colorLight: "#ffffff", //生成二维码的浅色部分
                    correctLevel: QRCode.CorrectLevel.L
                });
                qrcode.makeCode(_url);
            },
            _caculTotalMount: function () {
                let j = 0,k =0;
                vc.component.orderInfo.storeOrderCartDtos.forEach(item => {
                    if (!isNaN(item.cartNum) && !isNaN(item.price)) {
                        j += parseFloat(item.cartNum) * parseFloat(item.price).toFixed(2);
                        k += parseFloat(item.cartNum)
                    } else {
                        $that.orderInfo.totalMount = "请输入正确的数字";
                        $that.orderInfo.totalNum = "请输入正确的数字";
                        throw Error();
                    }
                });
                $that.orderInfo.totalMount = j.toFixed(2);
                $that.orderInfo.totalNum = k;
            },
        }
    });

})(window.vc);